package com.likl.service.impl;

import com.likl.domain.SMSCode;
import com.likl.service.SMSCodeService;
import com.likl.utils.CodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * ClassName:springboot_study
 * Description:
 *
 * @Create: 2024/7/12 14:32
 * @author: likl
 * @e-mail: likl1102@foxmail.com
 * @QQ: 2514927731
 */
@Service
public class SMSCodeServiceImpl implements SMSCodeService {

    @Autowired
    private CodeUtils codeUtils;

    @Override
//    @Cacheable(value = "smsCode", key = "#phone")
    @CachePut(value = "smsCode", key = "#phone")
    public String sendCodeToSMS(String phone) {
        return codeUtils.generator(phone);
    }

    @Override
    public boolean checkCode(SMSCode smsCode) {
        String code = smsCode.getCode();
        String cacheCode = codeUtils.get(smsCode.getPhone());
        return code.equals(cacheCode);
    }
}
